package com.yyz.controller;

import com.yyz.common.R;
import com.yyz.domain.AddressBook;
import com.yyz.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yyz
 * @create 2023-07-25 13:51
 */
@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;


    /**
     * 添加地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> insert(@RequestBody AddressBook addressBook) {
        log.info("{}", addressBook);

        addressBookService.insert(addressBook);

        return R.success("添加成功！");
    }

    /**
     * 查询全部地址
     *
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list() {
        List<AddressBook> addressBooks = addressBookService.list();

        return R.success(addressBooks);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook){
        log.info("{}",addressBook);

        addressBookService.setDefault(addressBook);

        return R.success(null);
    }

    /**
     * 获取默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        AddressBook addressBook = addressBookService.getDefault();

        return R.success(addressBook);
    }

    /**
     * 根据id查询地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> selectById(@PathVariable Long id){
        log.info("{}",id);

        AddressBook addressBook = addressBookService.selectById(id);

        return R.success(addressBook);
    }

    /**
     * 修改地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return R.success("修改成功！");
    }
}
